package com.servlet.login.root.service.impl;

import com.servlet.login.root.bean.User;
import com.servlet.login.root.dao.UserDAO;
import com.servlet.login.root.dao.impl.UserDAOImpl;
import com.servlet.login.root.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDAO userDAO = new UserDAOImpl();

    @Override
    public Boolean isExistsUser(String username) {
        if (username == null) {
            return false;
        }
        if (userDAO.queryByUserName(username) == null) {
            return false;
        }
        User user  = userDAO.queryByUserName(username);
        if (null == user.getUsername()||user.getUsername().length()<1) {
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Boolean isMatchePwd(String user, String password) {
        if (user == null) {
            return false;
        }
        User queryByUser = userDAO.queryByUserName(user);
        if (user.equals(queryByUser.getUsername()) && password.equals(queryByUser.getPassword())) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String returnResults(String username, String password) {
        String returnWord = new String();
        if (isExistsUser(username)) {
            if (isMatchePwd(username,password)) {
                returnWord = "用户匹配成功！";
                return returnWord;
            }
            returnWord = "密码不正确！";
            return returnWord;
        }
        returnWord = "不存在该用户请注册！";
        return returnWord;
    }

    @Override
    public User returnUser(String username) {
        return userDAO.queryByUserName(username);
    }


}
